import { request } from "../../request/index"

Page({
    data: {
        leftMenuList: [],
        rightContent: [],
        currentIndex: 0,
        scrollTop: 0
    },
    Cates: [],
    onLoad: function (options) {
        const Cates = wx.getStorageSync('cates');
        if (!Cates) {
            console.log(1)
            this.getCates()
        } else {
            if (Date.now() - Cates.times > 1000 * 10) {
                this.getCates()
            } else {
                this.Cates = Cates.data;
                let leftMenuList = this.Cates.map(v => v.cat_name)
                console.log(leftMenuList)
                let rightContent = this.Cates[0].children;
                this.setData({
                    leftMenuList,
                    rightContent
                })
            }

        }
    },
    async getCates() {
        const res = await request({
            url: '/categories'
        })
        this.Cates = res;
        wx.setStorageSync('cates', {
            times: Date.now(),
            data: this.Cates
        });
        let leftMenuList = this.Cates.map(v => v.cat_name)
        console.log(leftMenuList)
        let rightContent = this.Cates[0].children;
        this.setData({
            leftMenuList,
            rightContent
        })

    },
    handleItemTap(e) {
        const { index } = e.currentTarget.dataset;
        let rightContent = this.Cates[index].children;
        this.setData({
            currentIndex: index,
            rightContent,
            scrollTop: 0
        })
    }

})

